﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WZGL.Models;
namespace WZGL.Controllers
{
    public class HomeController : Controller
    {
        Model1 mo = new Model1();
        // GET: Default
        public ActionResult Index(int Type = 0, string name = "")
        {
            var list = mo.ProType.ToList();
            list.Insert(0, new ProType() { Name = "全部", ID = 0 });
            ViewBag.Type = new SelectList(list, "ID", "Name");
            var one = mo.ProInfo.Where(p => p.Name.Contains(name) && (Type == 0 || p.Type == Type)).ToList();
            return View(one);
        }
   
        [HttpPost]
        public ActionResult Edit(int? id)
        {

            if (id == null)
                return Json(false);

            var list = mo.ProInfo.FirstOrDefault(p => p.ID == id);
            if (list == null)
                return Json(false);

            list.IsDestroy = true;
            var res = mo.SaveChanges();
            if (res > 0)
            {
                return Json(res);
            }

            return Json(false);
        }
        public ActionResult Create()
        {
            ViewBag.Type = new SelectList(mo.ProType, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(ProInfo pi)
        {
            if (ModelState.IsValid)
            {
                if (pi != null)
                {
                    pi.IsDestroy = false;
                    mo.ProInfo.Add(pi);
                    var res = mo.SaveChanges() > 0;
                    if (res)
                    {
                        return RedirectToAction("Index");
                    }
                }
            }
            ViewBag.Type = new SelectList(mo.ProType, "ID", "Name");
            return View(pi);
        }
    }
}